package cn.com.generaldata.os.service.indicator_evaluation.gop_forecast;

import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 修正指数曲线预测海洋生产总值MAPE
 */
public class ModifiedExponentialCurveGOPMAPE extends IndicatorEvaluationCalculateService {

	private String forecastGOPIndicatorName = "修正指数曲线海洋生产总值";
	private String GOPIndicatorName = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;
	private int n = 5;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double r = 0d;
		int maxYear = getInputOrAccountingMaxYear(GOPIndicatorName, indicatorAccountVO.getAreaId());

		for (int i = 0; i < n; i++) {
			IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
			vo.setYear(maxYear - i);

			Double GOP = getGOP(vo);
			Double GOPForecast = getGOPForecast(vo);
			if (Double.valueOf(0).equals(GOPForecast)) {
				continue;
			}
			r += Math.abs((GOP - GOPForecast) / GOPForecast);

		}
		return r / n;
	}

	private Double getGOP(IndicatorAccountVO indicatorAccountVO) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				GOPIndicatorName);
		return result == null ? 0d : result;
	}

	private Double getGOPForecast(IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorEvaluationId(forecastGOPIndicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Double result = getEvaluationIndicatorValue(cri);
		return result == null ? 0d : result;
	}

}
